package org.lanqiao.tjut.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.tjut.bean.UsersBean;
import org.lanqiao.tjut.bean.UsersBeanStu;
import org.lanqiao.tjut.bean.UsersBeanTea;
import org.lanqiao.tjut.myUtils.MyUtils;
import org.lanqiao.tjut.service.ServiceFactory;

public class UserSelectCou extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码 防止中文乱码
		request.setCharacterEncoding("UTF-8");
		// 实例化
		UsersBeanTea usersBT = new UsersBeanTea();

		// 首次登陆与提交判定
		int my = MyUtils.obj2Int(request.getParameter("listLong"));

		if (my == 0) {

			// 获取用户number
			String strUnb = request.getParameter("Unumber");

			// 用户number存入实体类
			usersBT.setUsernum(strUnb);

			// 获取需要显示的所有的数据集合

			List<UsersBeanStu> stuCou = ServiceFactory.getUserInfoManagerStuServiceInstance().getAllStuCou(strUnb);
			// 将需要显示的结果集放入session缓存中
			request.getSession().setAttribute("stuCou", stuCou);

			List<UsersBeanStu> stuCouNo = ServiceFactory.getUserInfoManagerStuServiceInstance().getAllStuCouNo(strUnb);
			// 清空request缓存中的对象
			request.getSession().setAttribute("stuCouNo", stuCouNo);

			request.getSession().setAttribute("listLong", stuCou.size());
			request.getSession().setAttribute("listLongNo", stuCouNo.size());


		} else if (my != 0) {
			/**
			 * 课程批量处理
			 */
			request.setCharacterEncoding("UTF-8");

			// 已选课程数
			int list = MyUtils.obj2Int(request.getParameter("listLong"));
			// 未已选课程数
			int listN = MyUtils.obj2Int(request.getParameter("listLongNo"));

			// 获取用户number
			String strUnb = request.getParameter("userNum");
			usersBT.setUsernum(strUnb);

			// usersBT.setCouid(MyUtils.obj2Int(request.getParameter("selectCouid")));
			// 删除课程
			// 已选课程 chHobby选为 on 不选为 null
			for (int i = 1; i <= list; i++) {
				String ch = "chHobby" + i;
				String chHobby = request.getParameter(ch);
				// 寻找chHobby 为 null 的课程名称
				if (chHobby == null) {
					String Couid = "selectCouid" + i;
					// 取出相对用的课程ID
					usersBT.setCouid(MyUtils.obj2Int(request.getParameter(Couid)));

					ServiceFactory.getUserInfoManagerStuServiceInstance().deleteStuCou(usersBT);
				}
			}
			// 添加课程
			// 已选课程 chHobby选为 on 不选为 null
			for (int i = 1; i <= listN; i++) {
				String ch = "chHobbya" + i;
				String chHobbya = request.getParameter(ch);
				// 寻找chHobby 为 null 的课程名称
				if (chHobbya != null) {
					String Couid = "selectCouidNo" + i;
					// 取出相对用的课程ID
					usersBT.setCouid(MyUtils.obj2Int(request.getParameter(Couid)));

					// 导入默认id 成绩为空
					String stuCouSco = null;
					usersBT.setCousco(stuCouSco);

					ServiceFactory.getUserInfoManagerStuServiceInstance().addStuCou(usersBT);
				}
			}
			// 结果刷新 传回选择页面中
			List<UsersBeanStu> stuCou = ServiceFactory.getUserInfoManagerStuServiceInstance().getAllStuCou(strUnb);
			request.getSession().setAttribute("stuCou", stuCou);

			List<UsersBeanStu> stuCouNo = ServiceFactory.getUserInfoManagerStuServiceInstance().getAllStuCouNo(strUnb);
			request.getSession().setAttribute("stuCouNo", stuCouNo);



		}
		request.removeAttribute("usersB");
		// 转发到结果显示页面
		request.getRequestDispatcher("logined/UserInfoManager/userSelectCou.jsp").forward(request, response);
		// response.sendRedirect("logined/UserInfoManager/userInfoAddDetail.jsp");

	}

}
